package controller.packaging;

import java.sql.SQLException;

import service.CoffeeService;
import to.JobTO;
import util.CoffeeUtil;

public class BoxPackage extends Thread {
	private static CoffeeService service;

	public BoxPackage(CoffeeService service) {
		this.service = service;
	}

	@Override
	public void run() {
		while (true) {
			try {
				
				int bottlePackaging = service.selectMaterialTotalWeight("용기포장");
				while (bottlePackaging >= 20) {// DB에서 작업량을 가져옴 작업량이 20 이상일때
					service.updateMachineStatus("박스포장기", "포장중");
					System.out.println("박스포장 job = "+bottlePackaging);
					String sTime = CoffeeUtil.getNow();
					CoffeeUtil.sleep(CoffeeUtil.BOX_PACKAGING_TIME);
					String eTime = CoffeeUtil.getNow();
					JobTO jto = new JobTO("박스포장", sTime, eTime);
					jto.setWorkLoad(1);
					service.insertJobInfo(jto);
					// 박스포장 숫자 1 올리고 작업량 -20
					service.updateMaterialTotalWeight("박스포장", 1);
					service.updateMaterialTotalWeight("용기포장", -20);
					bottlePackaging = service.selectMaterialTotalWeight("용기포장");
				}
				service.updateMachineStatus("박스포장기", "대기중");
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
